#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "LCD1602.h"
#include "AT24C02.h"

unsigned char KeyNum;//全局变量不初始化，默认为0，局部变量为随机值
unsigned int Num;

void main()
{
	/*LCD_Init();
	LCD_ShowString(1,1,"hello");
	AT24C02_WriteByte(1,147);
	Delay(5);//这个也是看数据手册，因为写完之后要间隔 5 ms保证数据的写入
	Data = AT24C02_ReadByte(1);
	LCD_ShowNum(2,1,Data,3);
	*/
	
	LCD_Init();
	LCD_ShowNum(1,1,Num,5);
	while(1)
	{
		KeyNum=Key();
		if(KeyNum == 1)
		{
			Num++;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum == 2)
		{
			Num--;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum == 3)
		{
			AT24C02_WriteByte(0,Num%256);//低八位
			Delay(5);
			AT24C02_WriteByte(1,Num/256);//高八位
			Delay(5);//这个也是看数据手册，因为写完之后要间隔 5 ms保证数据的写入
			LCD_ShowString(2,1,"Write OK");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
		if(KeyNum == 4)
		{
			Num=AT24C02_ReadByte(0);//低八位
			Num|=AT24C02_ReadByte(1)<<8;//高八位
			LCD_ShowNum(1,1,Num,5);
			LCD_ShowString(2,1,"Read OK ");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
	}
}